package cn.jingyinghui.handler;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class ChatHandler extends TextWebSocketHandler {

    // 当菜农A说话时，这个方法会被自动调用
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        String msg = message.getPayload(); // 菜农A说的话
        System.out.println("收到消息: " + msg);

        // 用大喇叭喊给所有连接的菜农（广播）
        String reply = "【大喇叭】" + msg + "（已收到）";
        session.sendMessage(new TextMessage(reply));
    }
}
